package com.metacube.metaconnect.controller;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmail {

	/**
	 * sends a mail to the user
	 * 
	 * @param recipientEmailId
	 *            email_id of the user
	 * @param subject
	 *            subject of the email
	 * @param bodyText
	 *            body of the email
	 * @return true if mail successfully sent else returns false
	 */
	public boolean sendMail(String recipientEmailId, String subject,
			String bodyText) {

		Properties props = new Properties();

		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");

		Session session = Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(
								"metaconnect.metacube@gmail.com",
								"metacube#321");
					}
				});

		try {

			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(
					"metaconnect.metacube@gmail.com"));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(recipientEmailId));
			message.setSubject(subject);
			message.setText(bodyText);
			Transport.send(message);

		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}

		return true;
		// TODO Auto-generated method stub
	}
}
